﻿using AutoMapper;
using Entity.BaseInfo.DTO.Customer;
using Entity.BaseInfo.Entity;
using Entity.BaseInfo.View.Customer;
using Entity.Finance.DTO.MonthWages;
using Entity.Finance.DTO.MonthWagesItem;
using Entity.Finance.Entity;
using Entity.Finance.View.MonthWages;
using Entity.Finance.View.MonthWagesItem;
using FrameWork.DataContext;
using FrameWork.DataHelper;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Service
{
    public class MonthWagesService
    {
        /**
         * 数据库
         */
        private readonly DataContext _context;

        /**
         * 自动映射
         */
        private readonly IMapper _mapper;

        /**
         * 构造注入
         */
        public MonthWagesService(DataContext context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        /**
         * 获取列表
         */
        public async Task<RequestResponseModel<PageResult<MonthWagesView>>> GetMonthWagesList(MonthWagesSearchDTO model)
        {
            //筛选
            var query = _context.MonthWagesView
                .WhereIF(a => a.Code.Contains(model.Code), !string.IsNullOrEmpty(model.Code))
                .WhereIF(a => a.Year == model.Year, model.Year != null)
                .WhereIF(a => a.Month == model.Month, model.Month != null)
                ;

            //返回列表
            var list = await query
                .OrderByDescending(a => a.CreateTime)
                .Page(model.CurrentPage, model.PageSize)
                .ToListAsync();

            //总数据量
            var count = await query.CountAsync();

            //返回数据
            var pageResult = new PageResult<MonthWagesView>(list, count);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }

        /**
         * 获取子单列表
         */
        public async Task<RequestResponseModel<PageResult<MonthWagesItemView>>> GetMonthWagesItemList(MonthWagesItemSearchDTO model)
        {
            //筛选
            var query = _context.MonthWagesItemView
                .WhereIF(a => a.StaffCode.Contains(model.Staff) || a.StaffName.Contains(model.Staff), !string.IsNullOrEmpty(model.Staff))
                ;

            //返回列表
            var list = await query
                .OrderByDescending(a => a.CreateTime)
                .Page(model.CurrentPage, model.PageSize)
                .ToListAsync();

            //总数据量
            var count = await query.CountAsync();

            //返回数据
            var pageResult = new PageResult<MonthWagesItemView>(list, count);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }

        /**
         * 新增
         */
        public async Task<RequestResponseModel<string>> AddMonthWages(MonthWagesAddDTO model)
        {
            //新增数据
            var entity = _mapper.Map<MonthWagesModel>(model);
            entity.CreateTime = DateTime.Now;
            entity.Id = Guid.NewGuid().ToString();
            decimal amount = 0;
            foreach (var item in model.Items)
            {
                var entityItem = _mapper.Map<MonthWagesItemModel>(item);
                entityItem.Id = Guid.NewGuid().ToString();
                entityItem.MonthWagesId = entity.Id;
                entityItem.Year = model.Year;
                entityItem.Month = model.Month;
                entityItem.ActualMoney = entityItem.PayMoney + (entityItem.SocialSecurity ?? 0) + (entityItem.OtherMoney ?? 0);

                _context.MonthWagesItem.Add(entityItem);

                amount += entityItem.ActualMoney;
            }

            entity.Amount = amount;

            //更新表
            _context.MonthWages.Add(entity);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "新增成功", entity.Id);
        }

        ///**
        // * 更新
        // */
        //public async Task<RequestResponseModel<string>> PutMonthWages(MonthWagesUpdateDTO model)
        //{
        //    //校验数据
        //    var query = await Valid.ValidId<MonthWagesModel>(_context.MonthWages, model.Id);
        //    if (query == null)
        //    {
        //        return RequestResponse.CreateResponse(400, "Id无效", model.Id);
        //    }

        //    //编辑数据
        //    var entity = _mapper.Map(model, query);

        //    //更新表
        //    _context.MonthWages.Update(entity);
        //    await _context.SaveChangesAsync();

        //    return RequestResponse.CreateResponse(200, "更新成功", entity.Id);
        //}

        /**
         * 删除
         */
        public async Task<RequestResponseModel<string>> DeleteMonthWages(string Id)
        {
            //校验数据
            var query = await Valid.ValidId<MonthWagesModel>(_context.MonthWages, Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            }
            var items = await _context.MonthWagesItem.Where(a => a.MonthWagesId == Id).ToListAsync();
            _context.RemoveRange(items);
            //更新表
            _context.MonthWages.Remove(query);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "删除成功", Id);
        }
    }
}
